package us.wili.dev.netty;

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import us.wili.dev.common.mybatis.IdAndTimeAutoUpdatePlugin;
import us.wili.dev.common.util.snowflake.SnowflakeUtil;
import us.wili.dev.netty.filter.AlwaysCreateJwtTokenFilter;


@SpringBootApplication(
        exclude = {SecurityAutoConfiguration.class},
        scanBasePackages = {"us.wili.dev","us.wili.dev.service"}
)
@MapperScan({"us.wili.dev.dao", "us.wili.dev.netty.dao"})
public class ClientApplication {
    public static void main( String[] args ) throws Exception {

        /*SpringApplication netty = new SpringApplication(ClientApplication.class);
        ConfigurableApplicationContext context = netty.run(args);
        NettyTcpServer nettyTcpServer = context.getBean(NettyTcpServer.class);
        nettyTcpServer.bind();*/

        SpringApplication.run(ClientApplication.class, args);

    }


    @Bean
    public SocketIOServer socketIOServer() {
        Configuration config = new Configuration();
        config.setPort(1001);
        /**
         * 修复地址占用
         */
        SocketConfig socketConfig = config.getSocketConfig();
        socketConfig.setReuseAddress(true);

        /**
         * 超时处理
         */
        config.setTransports(Transport.WEBSOCKET, Transport.POLLING);
        config.setPingTimeout(11000);
        config.setPingInterval(3000);

        /**
         * 身份验证
         */
       /* config.setAuthorizationListener(new AuthorizationListener() {
            @Override
            public boolean isAuthorized(HandshakeData data) {
                String[] params = SymmetricEncoderUtils.AESDncode("AES", data.getSingleUrlParam("token")).split("\\_");
                if (params[params.length - 1] != null) {
                    return true;
                }
                return false;
            }
        });*/
        final SocketIOServer server = new SocketIOServer(config);
        return server;
    }
    //扫描socket的方法
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer) {
        return new SpringAnnotationScanner(socketIOServer);
    }




    /**
     * Mybatis插件，用于生成ID、版本号、创建时间、更新时间
     * @param idWorker
     * @return
     */
    @Bean
    public IdAndTimeAutoUpdatePlugin idAndTimeAutoUpdatePlugin(SnowflakeUtil idWorker, SnowflakeUtil versionWorkder) throws Exception {
        return new IdAndTimeAutoUpdatePlugin(idWorker, versionWorkder);
    }

    /**
     * token 生成策略过滤器
     * @return
     */
    @Bean
    public AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter() {
        return new AlwaysCreateJwtTokenFilter();
    }
    @Bean
    public FilterRegistrationBean alwaysCreateJwtTokenFilterRegistrationBean(AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter){
        FilterRegistrationBean registBean=new FilterRegistrationBean();
        registBean.setFilter(alwaysCreateJwtTokenFilter);
        registBean.addUrlPatterns("/pf-api/*");
        //order的数值越小 则优先级越高
        registBean.setOrder(6);
        return registBean;
    }
}
